<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * Short description for file
 *
 * Long description for file (if any)...
 *
 * PHP version 5
 *
 * Copyright 2008 The Picnic PHP Framework
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @category      Framework
 * @package       Pfw
 * @author        Sean Sitter <sean@picnicphp.com>
 * @copyright     2008 The Picnic PHP Framework
 * @license       http://www.apache.org/licenses/LICENSE-2.0
 * @link          http://www.picnicphp.com
 * @since         0.10
 */

Pfw_Loader::loadClass('Prj_Smarty_Standard');
Pfw_Loader::loadClass('Pfw_Controller_Base');

class Pfw_Controller_Standard extends Pfw_Controller_Base
{
    protected $front_controller=null;
    protected $smarty = null;
    protected $config = null;
    protected $params = array();

    public function __construct()
    {
        $this->setupView();
    }

    protected function setupView(){ }

    /**
     * Gets an instance of the view object
     *
     * @return Pfw_Smarty_Standard
     */
    public function getView()
    {
        if (null === $this->smarty) {
            $this->smarty = new Prj_Smarty_Standard();
        }
        return $this->smarty;
    }

    /**
     * Returns true if request was an http post
     *
     * @return bool  Request was a post?
     */
    public function isPost()
    {
        return Pfw_Request::isHttpPost();
    }

    /**
     * Returns true if request was an http get
     *
     * @return bool  Request was a get?
     */
    public function isGet()
    {
        return Pfw_Request::isHttpGet();
    }
    
    /**
     * Gets a request parameter
     * @param string $name
     * 
     * @return mixed
     */
    public function getParam($name)
    {
        return Pfw_Request::getParam($name);
    }

    /**
     * Gets the name => value pair array of request parameters
     * 
     * @return array
     */
    public function getParams()
    {
        return Pfw_Request::getParams();
    }

    /**
     * Returns true if request was ajax
     * NOTE - Only works if the javascript library sends the X_REQUESTED_WITH
     * header
     *
     * @return bool
     */
    public function isAjax()
    {
        return Pfw_Request::isAjax();
    }

    /**
     * Gets the url for a specific route name and route params
     * 
     * @param array $route_params controller, action, etc
     * @param string $route_name
     * @return string
     */
    public function urlFor($route_params = array(), $route_name = "default_action")
    {
        return $this->_getFrontController()->getRouter()->urlFor($route_name, $route_params);
    }

    /**
     * Sets the instance of the front controller which routed our request
     *
     * @param Pfw_Controller_Front $front_controller
     */
    public function _setFrontController($front_controller)
    {
        $this->front_controller = $front_controller;
    }

    /**
     * Gets the instance of the front controller which routed our request
     *
     * @return Pfw_Controller_Front
     */
    public function _getFrontController()
    {
        return $this->front_controller;
    }
    
    /**
     * Redirect to a specific route name and route params
     * 
     * @param array $route_params controller, action, etc
     * @param string $route_name
     */
    public function redirectTo($route_params = array(), $route_name = "default_action")
    {
        $this->redirectRelative($this->urlFor($route_params, $route_name));
    }

    /**
     * Redirects to a relative url with the same http host
     *
     * @param string $rel_url
     * @param string $qs
     * @param string $anchor
     */
    public function redirectRelative($rel_url, $qs = null, $anchor = null)
    {
        $qs_str = "";
        if($qs){
            foreach($qs as $key => $val){
                if($val){
                    $val = (string)$val;
                    $val = urlencode($val);
                    $qs_str .= "{$key}={$val}&";
                }
                else{
                    $qs_str .= $key;
                }
            }
            $qs_str = '?' . $qs_str;
        }
        $qs_str = rtrim($qs_str, '&');
        $anchor = $anchor ? "#{$anchor}" : '';

        $destination = "http://{$_SERVER['HTTP_HOST']}{$rel_url}{$qs_str}{$anchor}";
        $this->redirect($destination);
        exit();
    }

    /**
     * Redirect to the destination url
     *
     * @param string $destination
     */
    public function redirect($destination)
    {
        header('Location: '.$destination);
    }
}
